//
//  HTAlertOtherBoardView.m
//  TruckUser
//
//  Created by Ting Han on 2017/11/2.
//  Copyright © 2017年 Ting Han. All rights reserved.
//

#import "HTAlertOtherBoardView.h"
#import "HTAlertCell.h"
#import "HTAlertHeaderView.h"
#import "HTAlertFooterView.h"

@interface HTAlertOtherBoardView
()<UICollectionViewDelegate,UICollectionViewDataSource,UICollectionViewDelegateFlowLayout>
@property (weak, nonatomic) IBOutlet UICollectionView *collectionView;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *safeBottomHeight;

@property (weak, nonatomic) IBOutlet UICollectionViewFlowLayout *flowLayout;
@property (nonatomic,weak) HTTextView *textView;
@property (nonatomic,assign) CGFloat off;

@property (nonatomic,assign) NSInteger selectedFirstIndex;
@property (nonatomic,assign) NSInteger selectedSecondIndex;


@property (nonatomic,strong) NSArray *data1;
@property (nonatomic,strong) NSArray *data2;
@property (nonatomic,strong) NSArray *data3;
@property (nonatomic,strong) NSArray *headData;
@end
@implementation HTAlertOtherBoardView

- (void)awakeFromNib{
    [super awakeFromNib];
    [self configCollectionView];
    [self notifi];
    self.selectedFirstIndex = -1;
    self.selectedSecondIndex = -1;
}
- (NSString *)remarkString{
    if (self.textView.text == nil || self.textView.text.length <= 0) {
        return @"";
    }
    return self.textView.text;
}
- (NSString *)payTypeString{
    if (self.selectedSecondIndex >= 0) {
        return  [NSString stringWithFormat:@"%@",self.data2[self.selectedSecondIndex]];
    }
    return @"";
}
- (NSString *)addTypeString{
    if (self.selectedFirstIndex >= 0) {
        return  [NSString stringWithFormat:@"%@",self.data1[self.selectedFirstIndex]];
    }
    return @"";
}
- (NSString *)totalMessage{
    NSString *total  = nil;
    if (self.selectedFirstIndex >= 0) {
      total = [NSString stringWithFormat:@"%@",self.data1[self.selectedFirstIndex]];
    }
    if (self.selectedSecondIndex >= 0) {
      total = [NSString stringWithFormat:@"%@ %@",total,self.data2[self.selectedSecondIndex]];
    }
    if (self.textView.text == nil || self.textView.text.length <= 0) {
        return total;
    }
    total = [NSString stringWithFormat:@"%@ %@",total,self.textView.text];
    return total;
}
- (void)configCollectionView{
    self.backgroundColor = [[UIColor blackColor]colorWithAlphaComponent:0.5];
    self.safeBottomHeight.constant = IPHONE_SAFEBOTTOMAREA_HEIGHT; self.collectionView.allowsMultipleSelection = YES;
    self.headData = @[@"装卸方式",@"付款方式",@"备注"];
    self.data1 = @[@"一装一卸",@"一装两卸",@"一装多卸",@"两装一卸",@"两装两卸",@"两装多卸"];
    self.data2 = @[@"全现金",@"需回单",@"三段付"];
    self.data3 = @[@"三不超",@"高价急走",@"需要雨布",@"货物重量为估算"];
    [self.flowLayout setScrollDirection:UICollectionViewScrollDirectionVertical];
    CGFloat width = (kHTScreenWidth - 50)/4.0;
    self.flowLayout.itemSize = CGSizeMake(width, kWidth(30));
    self.flowLayout.sectionInset = UIEdgeInsetsMake(0, 10, 0, 10);
    self.flowLayout.minimumLineSpacing = 15;
    self.flowLayout.minimumInteritemSpacing = 10;
    self.collectionView.backgroundColor = [UIColor whiteColor];
    [self.collectionView registerNib:[UINib nibWithNibName:@"HTAlertCell" bundle:nil] forCellWithReuseIdentifier:AlertCell];
    [self.collectionView registerNib:[UINib nibWithNibName:@"HTAlertHeaderView" bundle:nil] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:AlertHeaderView];
    [self.collectionView registerNib:[UINib nibWithNibName:@"HTAlertFooterView" bundle:nil] forSupplementaryViewOfKind:UICollectionElementKindSectionFooter withReuseIdentifier:AlertFooterView];
    self.collectionView.delegate = self;
    self.collectionView.dataSource = self;
    
}

- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{
    return 3;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    HTAlertCell *alert = [collectionView dequeueReusableCellWithReuseIdentifier:AlertCell forIndexPath:indexPath];
    if (indexPath.section == 0) {
        alert.titleLabel.text = self.data1[indexPath.row];
    }
    if (indexPath.section == 1) {
        alert.titleLabel.text = self.data2[indexPath.row];
    }
    return alert;
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    if (section == 0) {
        return self.data1.count;
    }
    if (section == 1) {
        return self.data2.count;
    }
    return 0;
}
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section{
    return CGSizeMake(kHTScreenWidth , kWidth(40));
}
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForFooterInSection:(NSInteger)section{
    if (section == 2) {
        return CGSizeMake(kHTScreenWidth , kWidth(200));
    }
    return CGSizeZero;
}

- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath{
    UICollectionReusableView *resualView = nil;
    
    if ([kind isEqualToString: UICollectionElementKindSectionHeader]) {
        HTAlertHeaderView *header = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:AlertHeaderView forIndexPath:indexPath];
        header.titleLabel.text = self.headData[indexPath.section];
        resualView = header;
    }
    if ([kind isEqualToString:UICollectionElementKindSectionFooter] && indexPath.section == 2) {
        HTAlertFooterView *footer = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionFooter withReuseIdentifier:AlertFooterView forIndexPath:indexPath];
        footer.dataSource = self.data3;
        self.textView = footer.textView;
        resualView = footer;
    }
    return resualView;
}
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    switch (indexPath.section) {
        case 0:
        {
            if (self.selectedFirstIndex >= 0 &&self.selectedFirstIndex != indexPath.row) {
                [collectionView deselectItemAtIndexPath:[NSIndexPath indexPathForRow:self.selectedFirstIndex inSection:indexPath.section] animated:YES];
            }
            [collectionView selectItemAtIndexPath:indexPath animated:YES scrollPosition:UICollectionViewScrollPositionNone];
            self.selectedFirstIndex = indexPath.row;
        }
            break;
        case 1:
        {
            if (self.selectedSecondIndex >= 0 && self.selectedSecondIndex != indexPath.row) {
                [collectionView deselectItemAtIndexPath:[NSIndexPath indexPathForRow:self.selectedSecondIndex inSection:indexPath.section] animated:YES];
            }
            [collectionView selectItemAtIndexPath:indexPath animated:YES scrollPosition:UICollectionViewScrollPositionNone];
            self.selectedSecondIndex = indexPath.row;
        }
            break;
        default:
            break;
    }

}
- (void)notifi{
    NSNotificationCenter *defaultCenter = [NSNotificationCenter defaultCenter];
    
    [defaultCenter addObserver:self selector:@selector(keyboardWillShowOrHide:) name:UIKeyboardWillShowNotification object:nil];
    [defaultCenter addObserver:self selector:@selector(keyboardWillShowOrHide:) name:UIKeyboardWillHideNotification object:nil];
    
}
- (void)dealloc {
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}
- (void)keyboardWillShowOrHide:(NSNotification *)notification
{
    
    //    //获取通知名
    NSString *notificationName = notification.name;
    
    if ([notificationName isEqualToString:UIKeyboardWillShowNotification]) {
        NSDictionary *keyboardInfo = notification.userInfo;
        CGRect keyboardF = [keyboardInfo[UIKeyboardFrameEndUserInfoKey] CGRectValue];
        CGRect fra = [self convertRect:keyboardF fromView:nil];
        CGFloat keyboardHeight = fra.size.height;
        
        CGRect selfFrameFromUIWindow = [self.textView convertRect:self.textView.bounds toView:nil];
        // textField底部距离屏幕底部的距离
        CGFloat bottomHeight =kHTScreenHeight - selfFrameFromUIWindow.origin.y - selfFrameFromUIWindow.size.height;
        CGFloat off =keyboardHeight - bottomHeight + 10;
        if(off > 0)
            
        {  self.off = off;
            CGPoint scrollPoint = CGPointMake(0.0, off);
            [self.collectionView setContentOffset:scrollPoint animated:YES];
        }
    }
    if ([notificationName isEqualToString:UIKeyboardWillHideNotification]) {
        if (self.off > 0) {
            CGPoint scrollPoint = CGPointMake(0.0, 0);
            [self.collectionView setContentOffset:scrollPoint animated:YES];
        }
    }
}

@end
